package com.skp.shaphan.actions;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.skp.job.ProjectJobException;
import com.skp.shaphan.ui.SQLEditor;
import com.skp.shaphan.ui.results.IResultPane;
import com.skp.shaphan.ui.results.ResultPane;

/**
 * Shows all the tables in a connection
 * @author Stephen Phillips
 *
 */
public class ShowMaintAction implements Runnable {
	private SQLEditor editor;
	private IResultPane resultPane;
	
	public ShowMaintAction() {
		
	}
	
	@Override
	public void run() {
		Connection connection = editor.getConnection().getConnection();
		String sql = "select * from ps_maintenance_log" +
			" where update_id in (" +
			"     (select max(update_id) from ps_maintenance_log where lower(descrlong) like lower('HRMS % Bundle%'))" +
			"  union" +
			"     (select max(update_id) from ps_maintenance_log where lower(descr) like lower('Patch for PeopleTools%'))" +
			"  union" +
			"     (select max(update_id) from ps_maintenance_log where lower(descrlong) like lower('%Tax Update%'))" +
			"  union" +
			"     (select max(update_id) from ps_maintenance_log where lower(descrlong) like lower('%Maintenance Pack%'))" +
			")" +
			" order by update_id";
		ResultSet results;
		try {
			PreparedStatement st = connection.prepareStatement(sql);
			results = st.executeQuery();
		} catch (SQLException e) {
			(new ProjectJobException(e)).displayException();
			return;
		}
		resultPane.setResultSet("", results);
	}

	public SQLEditor getEditor() {
		return editor;
	}

	public void setEditor(SQLEditor editor) {
		this.editor = editor;
	}

	public IResultPane getResultPane() {
		return resultPane;
	}

	public void setResultPane(IResultPane iResultPane) {
		this.resultPane = iResultPane;
	}

}
